package UI;
import Ctr.*;
import Model.*;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.Scanner;
import java.util.Random;
import java.util.ArrayList;
/**
 * Write a description of class SalgUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SalgUI
{
    private ArrayList<Integer> generatedList;
    private Random randomGenerator;
    private Ordre current;
    private VareCtr vare;
    private OrdreCtr ordre;
    private ArrayList<String> fak;
    private double total = 0;
    private int nummer = 0;

    /**
     * Constructor for objects of class SalgUI
     * @
     */
    public SalgUI()
    {
        fak = new ArrayList<String>();
        randomGenerator = new Random();
        generatedList = new ArrayList<Integer>();
        vare = new VareCtr();
        ordre = new OrdreCtr();

    }

    /**
     * this method starts up the salgsMenu where we can sell items
     * 
     * @param nothing
     * @return nothing
     */
    public void start()
    {
        salgsMenu();
    }

    /**
     * this method makes it possible to step further in the program 
     * by selecting one of the numbers.
     * 
     * @param nothing
     * @return nothing
     */
    public void salgsMenu()
    {
        boolean exit = false;
        while(!exit)
        {
            int choise = writeSalgsMenu();
            if(choise == 1) // start salg, opret ordre
            {
                createOrdre();
                addVare();
                faktura();
            }
            else if(choise == 2) // start salg, opret ordre
            {
                afslutSalg();
                exit = true;
            }
            else if(choise == 3) // start salg, opret ordre
            {
                exit = true;
            }
        }
    }

    /**
     * this method shows the user his choises. it returns choise which
     * holds the choise of number
     * 
     * @param nothing
     * @return choise
     */
    public int writeSalgsMenu()
    {
        Scanner keyboard = new Scanner(System.in);

        System.out.println("\fVelkommen til Vestbjerg Byggecenter");
        System.out.println("-----------------------------------");
        System.out.println("(1)AddVare, (2)AfslutSalg, (3)Afslut");
        afslutSalg();
        int choise = keyboard.nextInt();
        return choise;       
    }

    /**
     * this method allows the user to make an input to define the amount
     * of a vare
     * 
     * @param nothing
     * return status
     */
    public int inputAntal()
    {
        // makes an object keyboard to have input from the screen
        Scanner keyboard = new Scanner(System.in);  
        System.out.println(" Antal:  ");
        int status = keyboard.nextInt();
        return status;
    }

    /**
     * this method allows the user to input a vareNr
     * 
     * @param nothing
     * @return status
     */
    public int inputVare()
    {
        // makes an object keyboard to have input from the screen
        Scanner keyboard = new Scanner(System.in);  
        System.out.println(" Vare Nummer:  ");
        int status = keyboard.nextInt();
        return status;
    }

    /**
     * this method let us make an input for the delordreNr
     * 
     * @param nothing
     * @return status
     */
    public int inputDel()
    {
        // makes an object keyboard to have input from the screen
        nummer++;
        return nummer;
    }

    /**
     * this method creats an ordreNr through generating a randomnized number
     * 
     * @param nothing
     * @return index
     */
    public int inputOrdreNr()
    {
        int index = randomGenerator.nextInt(999999);
        if(inputOrdreNrChecker(index))
        {
            inputOrdreNr();
        }
        return index;
    }

    /**
     * this method checks for if the ordrenummer which was just generated is used
     * before
     * 
     * @param index
     * @return boolean used
     */
    public boolean inputOrdreNrChecker(int index)
    {
        boolean used = false;
        for(Integer i : generatedList)
        {
            if(i.equals(index))
            {
                used = true;
            }
        }
        return used;
    }

    /**
     * this method makes it possible to delete a delordre from the ordre
     * 
     * @param nothing
     * @return nothing
     */
    public void removeVare()
    {
        current.removeDelordre(inputAntal());
    }

    /**
     * this method creates the ordre which is about to be used
     * 
     * @param nothing
     * @return nothing
     */
    public void createOrdre()
    {
        if(current == null)
        {
            int nr = inputOrdreNr();
            String dato = DateUtils.now();
            current = new Ordre(nr,dato);
            ordre.addOrdre(current);
        }
    }

    /**
     * this method adds a new vare to the collection 
     * 
     * @param nothing
     * @return nothing
     */
    public void addVare()
    {
        Delordre p = new Delordre(inputAntal(),inputVare(),inputDel());
        current.addDelordre(p);
        total += current.totalPris();
    }

    /**
     * this method prints out all the information about the purcahse. 
     * 
     * @param nothing
     * @return nothing
     */
    public void faktura()
    {
        System.out.println(current.myFaktura());
        fak.add(current.myFaktura());

    }

    /**
     * this method prints out every Faktura and print out the total prize
     * @param nothing
     * @return nothing
     */
    public void afslutSalg()
    {
        for(String s : fak)
        {
            System.out.println(s);
        }
        if(current != null)
        {
            System.out.println("\nTotal Pris: " + total);
        }
    }

    /**
     * this method make it possible to get getOrdreNr from Ordre class
     * @param nothing
     * @return current.getOrdreNr()
     */
    public int getCurrent()
    {
        return current.getOrdreNr();
    }
}
